package com.db.dbdocument.generateDB.query.clickhouse;

import com.db.dbdocument.generateDB.common.DataSoreceConnect;
import com.db.dbdocument.generateDB.util.StrUtils;
import com.db.dbdocument.generateDB.vo.DbTableInfoVo;
import com.db.dbdocument.generateDB.vo.DbTableStructureInfoVo;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ClickhouseDataBaseQuery {


    public static List<DbTableInfoVo> getTableInfo(List<Map<String,Object>> dbTableList){

        List<DbTableInfoVo> dbTableInfoVoList = new ArrayList<>();
        for (Map<String, Object> dbTable : dbTableList){
            DbTableInfoVo dbTableInfoVo = new DbTableInfoVo();
            dbTableInfoVo.setTableName(dbTable.getOrDefault("TABLE_NAME","").toString());
            dbTableInfoVo.setEngine(StrUtils.valueOf(dbTable.getOrDefault("ENGINE","")));
            dbTableInfoVo.setTableComment(StrUtils.valueOf(dbTable.getOrDefault("REMARKS","")));
            dbTableInfoVo.setTableCollation(StrUtils.valueOf(dbTable.getOrDefault("TABLE_COLLATION","")));
            dbTableInfoVo.setTableCollation(StrUtils.valueOf(dbTable.getOrDefault("TABLE_COLLATION","")));
            dbTableInfoVo.setCreateTime(StrUtils.valueOf(dbTable.getOrDefault("CREATE_TIME","")).replace("T",""));
            dbTableInfoVoList.add(dbTableInfoVo);
        }

        return dbTableInfoVoList;
    }



    public static List<DbTableStructureInfoVo> getTableStructureInfo(Connection connection) throws SQLException {
        //数据库名称
        String catalog = connection.getCatalog();

        //获取所有表结构
        String tableStructureSql = "select database,\n" +
                "table,\n" +
                "name,\n" +
                "type,\n" +
                "default_kind,\n" +
                "default_expression,\n" +
                "comment from system.columns \n" +
                "where database = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(tableStructureSql);
        preparedStatement.setString(1,catalog);
        ResultSet resultSet = preparedStatement.executeQuery();

        List<Map<String, Object>> dbTableStructureList  = DataSoreceConnect.rsConvertListMap(resultSet);

        List<DbTableStructureInfoVo> dbTableStructureVoList = new ArrayList<>();
        for (Map<String,Object>  dbTableStructure:dbTableStructureList){
            DbTableStructureInfoVo dbTableStructureVo = new DbTableStructureInfoVo();

            dbTableStructureVo.setTableSchema(dbTableStructure.getOrDefault("database","").toString());
            dbTableStructureVo.setTableName(dbTableStructure.getOrDefault("table","").toString());
            dbTableStructureVo.setColumnName(dbTableStructure.getOrDefault("name","").toString());
            dbTableStructureVo.setColumnType(dbTableStructure.getOrDefault("type","").toString());
            dbTableStructureVo.setColumnDefault(StrUtils.valueOf(dbTableStructure.getOrDefault("default_expression","")));
            dbTableStructureVo.setColumnComment(StrUtils.valueOf(dbTableStructure.getOrDefault("comment","")));

            dbTableStructureVoList.add(dbTableStructureVo);
        }

        DataSoreceConnect.close(resultSet);

        return dbTableStructureVoList;

    }

}
